package com.operator.Activitys.activity;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.operator.Activitys.R;
import com.operator.Activitys.activity.base.BaseActivity;
import com.operator.Activitys.bean.User;
import com.operator.Activitys.http.manager.HomeManager;
import com.operator.Activitys.utils.Constant;
import com.operator.Activitys.utils.FrescoUtils;
import com.operator.Activitys.utils.ToastUtils;

import org.simple.eventbus.Subscriber;
import org.simple.eventbus.ThreadMode;

import butterknife.Bind;

/**
 * 项目名称：金泽云 手机app
 * 类描述：通讯供求 详情
 * 创建人：张飞祥
 * 创建时间：2018/6/1 16:41
 * 修改人：张飞祥
 * 修改时间：2018/6/1 16:41
 * 修改备注：
 */
public class SupplyDetailsActivity extends BaseActivity {

    //返回按钮
    @Bind(R.id.iv_return)
    ImageView ivReturn;
    //标题
    @Bind(R.id.tv_title)
    TextView tvTitle;
    //设置头像
    @Bind(R.id.sv_my_user_icon)
    SimpleDraweeView svMyUserIcon;
    //设置店铺名称
    @Bind(R.id.tv_shop_name)
    TextView tvShopName;
    //设置联系电话
    @Bind(R.id.tv_shop_phone)
    TextView tvShopPhone;
    //设置所属区域
    @Bind(R.id.tv_shop_region)
    TextView tvShopRegion;
    //设置店铺地址
    @Bind(R.id.tv_shop_address)
    TextView tvShopAddress;
    //设置店铺固定电话
    @Bind(R.id.tv_shop_fixed_phone)
    TextView tvShopFixedPhone;

    //店铺名称
    private String mShopName = "";
    //TODO 初始化网络请求
    public HomeManager mApiManager;

    @Override
    protected void initUI() {
        setContentView(R.layout.act_supply_details);
        //设置状态栏为透明
        setThemeTransparent();
        //网络请求接口 常驻方法
        mApiManager = mApiManager.getHomeApiManager(this,this.getTaskId());
        //标题
        tvTitle.setText("供求详情");
        //显示返回按钮
        ivReturn.setVisibility(View.VISIBLE);
        //返回 按钮点击事件
        ivReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        //TODO 获取上个页面 传递过来的 店铺名称
        Bundle bundle = this.getIntent().getExtras();
        if (bundle != null)
            mShopName = bundle.getString(Constant.INTENT_SUPPLY_SHOP_NAME);
    }

    @Override
    protected void initData() {
        //弹出 加载提示
        initShowTimeDialog("正在加载店铺信息，请稍候....",this);
        //开始网络请求
        if (!mShopName.equals("")){
            mApiManager.homeSupplyDetails(mShopName);
        }
    }


    /**
     * 获取 通讯供求 店铺信息 报错
     */
    @Subscriber(tag = Constant.BUS_HOME_SUPPLY_DETAILS_ERROR, mode = ThreadMode.MAIN)
    public void initSupplyDetailsError(String httpError) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //隐藏dialog
                timeDialog.dismiss();
                //弹出提示
                ToastUtils.showShort(SupplyDetailsActivity.this,"加载失败，网络连接不稳定！");
            }
        }, 2000);
    }


    /**
     * 获取 通讯供求 店铺信息
     */
    @Subscriber(tag = Constant.BUS_HOME_SUPPLY_DETAILS, mode = ThreadMode.MAIN)
    public void initSupplyDetails(final User user) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //隐藏dialog
                timeDialog.dismiss();
                //加载数据
                if (user != null){
                    //设置头像
                    FrescoUtils.loadImage(user.getImgUrl(),svMyUserIcon);
                    //设置店铺名称
                    tvShopName.setText(user.getStoreName());
                    //设置联系电话
                    tvShopPhone.setText(user.getMobile());
                    //设置所属区域
                    tvShopRegion.setText(user.getArea());
                    //设置店铺地址
                    tvShopAddress.setText(user.getAddress());
                    //设置店铺固定电话
                    tvShopFixedPhone.setText(user.getLinkTel());
                }else{
                    //弹出提示
                    ToastUtils.showShort(SupplyDetailsActivity.this,"获取店铺信息失败！");
                }
            }
        }, 1000);
    }
}
